<?php
class UsersController extends AppController {
	public $name = 'Users';
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('login', 'logout');
	}
	
	public function login() {
		if (!isset($this->request->query['access_token'])) {
			$config = array_merge(
				Configure::read('Meshtiles.config'), 
				array('callbackURL' => (env('HTTPS') ? 'https://' : 'http://') .  env('SERVER_NAME') . $this->request->here)
			);
			$this->redirect(API_SERVER . '/oauth/authorize?' .http_build_query($config));
		} else {
			if ($this->Auth->login()) {
				$this->redirect($this->Auth->loginRedirect);
			}
		}
	}
	
	public function logout() {
		//$this->autoRender = false;
		$this->Auth->logout();
		//$this->redirect($this->Auth->logoutRedirect);
	}
}